package cn.com.oceansoft.business.danger.service;

import cn.com.oceansoft.business.base.entity.Pager;
import cn.com.oceansoft.business.base.entity.Query;
import cn.com.oceansoft.business.base.entity.Result;
import cn.com.oceansoft.business.danger.entity.*;
import cn.com.oceansoft.business.danger.persistence.ICollecterInfoMapper;
import cn.com.oceansoft.business.danger.util.ExeclUtil;
import cn.com.oceansoft.nos.common.util.MD5Util;
import cn.com.oceansoft.nos.common.util.web.PageUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2018/5/3.
 */
@Service
public class CollecterInfoServiceImpl implements ICollecterInfoService {
    @Resource
    private ICollecterInfoMapper collecterInfoMapper;
    @Override
    public Result loginAuthen(CollecterInfo collecterInfo) {
        List<CollecterInfo> list = new ArrayList<CollecterInfo>();
        try {
            collecterInfo.setPassword(MD5Util.getMD5String(collecterInfo.getPassword()));
            list=collecterInfoMapper.getLogin(collecterInfo);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (list.size() > 0) {
            CollecterInfo _u = list.get(0);
            _u.setPassword("");
            return _u.getDelflag().equals("0") ? new Result(_u) : new Result(
                    Result.FAILURE, "当前账户不可用，请与管理员联系");
        }

        return new Result(Result.FAILURE, "用户名或密码错误");
    }

    @Override
    public CollecterInfo getUser(CollecterInfo collecterInfo) {
        try{
            return collecterInfoMapper.getUser(collecterInfo);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Pager<CollecterInfo> getList(Query query) {
        try{
            PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
            Page<CollecterInfo> page = (Page<CollecterInfo>) collecterInfoMapper.getList(query);
            return PageUtil.page2pager(page, query);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Pager<CollecterInfo> getList2(Query query) {
        try{
            PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
            Page<CollecterInfo> page = (Page<CollecterInfo>) collecterInfoMapper.getList2(query);
            return PageUtil.page2pager(page, query);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<CollecterInfo> getdict(CollecterInfo collecterInfo) {
        try{
            return collecterInfoMapper.getdict(collecterInfo);
        }catch (Exception e){

        }
        return null;
    }

    @Override
    public List<CollecterInfo> getonedict(CollecterInfo collecterInfo) {
        try{
            return collecterInfoMapper.getonedict(collecterInfo);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<CollecterInfo> getjl(CollecterInfo collecterInfo) {
        try{
            return collecterInfoMapper.getjl(collecterInfo);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Result updateRole(CollecterInfo collecterInfo) {
        Result result=new Result();
        try{
            collecterInfoMapper.updateRole(collecterInfo);
            result.setSucc(true);
            result.setMsg("激活成功！");
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        result.setSucc(false);
        result.setMsg("激活失败！");
        return result;
    }

    @Override
    public Result updatePassword(CollecterInfo collecterInfo) {
        if(null==collecterInfo){
            return new Result(Result.FAILURE, "更新失败");
        }
        if(StringUtils.hasText(collecterInfo.getPassword()) && StringUtils.hasText(collecterInfo.getOldpassword())){
            collecterInfo.setPassword(MD5Util.getMD5String(collecterInfo.getPassword()));
            collecterInfo.setOldpassword(MD5Util.getMD5String(collecterInfo.getOldpassword()));
            CollecterInfo collecterInfo1=collecterInfoMapper.getUser(collecterInfo);
            if(collecterInfo1!=null&&collecterInfo.getOldpassword().equals(collecterInfo1.getPassword())){
                try{
                    collecterInfoMapper.updatePassword(collecterInfo);
                    return new Result();
                }catch (Exception e){
                    e.printStackTrace();
                    return new Result(Result.FAILURE, "修改密码失败");
                }
            }else{
                return new Result(Result.FAILURE, "原始密码错误");
            }
        }else{
            return new Result(Result.FAILURE, "原始密码或新密码不能为空");
        }
    }


    @Override
    public List<Qytj> qhtj(int deep, Query query) {
        if(deep==1){
            return collecterInfoMapper.qhtj(query);
        }else if(deep==2){
            return collecterInfoMapper.jdtj(query);
        }else if(deep==3){
            return collecterInfoMapper.sqtj(query);
        }else{
            return collecterInfoMapper.oneSqtj(query);
        }
    }

    @Override
    public List<TypeTj> typetj(int deep, Query query) {
        if(deep==1){
            return collecterInfoMapper.typetj1(query);
        }else if(deep==2){
            return collecterInfoMapper.typetj2(query);
        }else if(deep==3){
            return collecterInfoMapper.typetj3(query);
        }else{
            return collecterInfoMapper.typetj4(query);
        }
    }

    @Override
    public List<XxTj> xxtj(int deep, Query query) {
        if(deep==1){
            return collecterInfoMapper.xxtj1(query);
        }else if(deep==2){
            return collecterInfoMapper.xxtj2(query);
        }else if(deep==3){
            return collecterInfoMapper.xxtj3(query);
        }else{
            return collecterInfoMapper.xxtj4(query);
        }
    }

    @Override
    public List<DangerContent> getChildtype(DangerContent dangerContent) {
        String i =(Integer.parseInt(dangerContent.getType())+134)+"";
        List<DangerContent> dc = collecterInfoMapper.getDict(i);
        return dc;
    }

    public void exportXxtj(HttpServletResponse response, List<XxTj> list,int deep) {
        String sheet1="详细统计表";
        String[] sheets=new String[]{sheet1};
        Map<String,String[]> titles=new HashMap<>();
        String ss = "区";
        if(deep==1){
            ss = "区";
        }else if(deep==2){
            ss = "街道";
        }else {
            ss = "社区";
        }
        String[] title=new String[]{"序号",ss,"拆除隔离（m^2）","拆铁珊栏（m^2）","拆违建（m^2）","联合执法（次）","停产停业（家）","停租（家）","停电、停水（家）","责令停止使用（家）","查封（家）","搬离违规居住（人）","约谈当事人（人）","警告(人)","罚款（元）","拘留（人）","刑事拘留（人）","取保后审（人）","约谈涉事负责人（人）","责任追究（人）"};
        titles.put(sheet1,title);
        Map<String,List<String[]>> values=new HashMap<>();
        List<String[]> value=new ArrayList<>();
        for(int i=0;i<list.size();i++){
            XxTj xxTj = list.get(i);
            String[] str = new String[]{i+1+"",xxTj.getName(),xxTj.getGlmj()+"",
                    xxTj.getZlmj()+"",xxTj.getWjmj()+"",xxTj.getLhzf()+"",xxTj.getTytc()+"",
                    xxTj.getTz()+"",xxTj.getTstd()+"",xxTj.getZltzsy()+"",xxTj.getCf()+"",
                    xxTj.getBlwgjz()+"",xxTj.getYtdsr()+"",xxTj.getJgrs()+"",xxTj.getFkje()+"",
                    xxTj.getXzjlrs()+"",xxTj.getXsjlrs()+"",xxTj.getQbhsrs()+"",xxTj.getYtssfzr()+"",
                    xxTj.getZrzj()+""};
            value.add(str);
        }
        values.put(sheet1,value);
        ExeclUtil.createExecl("详细统计表",sheets,titles,values,response);
    }

    @Override
    public void exportQytj(HttpServletResponse response, List<Qytj> list,int deep) {
        String sheet1="区域统计表";
        String[] sheets=new String[]{sheet1};
        Map<String,String[]> titles=new HashMap<>();
        String ss = "区";
        if(deep==1){
            ss = "区";
        }else if(deep==2){
            ss = "街道";
        }else {
            ss = "社区";
        }
        String[] title=new String[]{"序号",ss,"上报数",
                "未审核",
                "信息不属实",
                "重复上报",
                "已审核",
                "流转数",
                "直接整改",
                "处罚后整改",
                "未反馈",
                "街道确认总数",
                "区确认总数",
                "整改率(%)"};
        titles.put(sheet1,title);
        Map<String,List<String[]>> values=new HashMap<>();
        List<String[]> value=new ArrayList<>();
        for(int i=0;i<list.size();i++){
            Qytj qytj = list.get(i);
            String[] str = new String[]{i+1+"",qytj.getName(),qytj.getSbs()+"",
                    qytj.getWsh()+"",qytj.getThsbss()+"",qytj.getThscf()+"",qytj.getYsh()+"",
                    qytj.getLzs()+"",qytj.getZjzg()+"",qytj.getCfzg()+"",qytj.getWfk()+"",
                    qytj.getJdqrs()+"",qytj.getQqrs()+"",qytj.getZgl()};
            value.add(str);
        }
        values.put(sheet1,value);
        ExeclUtil.createExecl("区域统计表",sheets,titles,values,response);
    }

    @Override
    public void exportTypetj(HttpServletResponse response, List<TypeTj> list,int deep) {
        String sheet1="类型统计表";
        String[] sheets=new String[]{sheet1};
        Map<String,String[]> titles=new HashMap<>();
        String ss = "区";
        if(deep==1){
            ss = "区";
        }else if(deep==2){
            ss = "街道";
        }else {
            ss = "社区";
        }
        String[] title=new String[]{"序号",ss,"生产、经营、储存区与住宿区未采用实体墙分隔",
                "居住房屋违法生产、经营、储存易燃易爆危险品",
                "违法建筑、违法搭建、门窗铁栅栏",
                "住宿区域违法群租",
                "电动自行车在室内公共空间停放、充电",
                "疏散通道和安全出口占用、堵塞",
                "住宿场所未按规定设置消防设施、器材",
                "住宿场所未明确消防安全管理责任人",
                "三合一总数",
                "出租人、转租人放任出租房屋消防安全管理",
                "违法建筑和非居住场所出租居住",
                "擅自改变房屋原有结构、装修分隔及违法群租",
                "出租房屋用于生产、经营、储存易燃易爆危险品",
                "出租房卧室内使用明火",
                "出租房屋擅自安装门窗障碍物",
                "电动自行车在室内公共空间停放、充电",
                "出租房总数",
                "生产、销售不合格电动自行车",
                "从非法渠道购买电动自行车及电瓶",
                "私自改装电动自行车",
                "在不合规地下室停放电动自行车",
                "电动自行车在室内公共空间停放、充电",
                "物业管理单位放任电动自行车违规停放、充电",
                "电动车总数",
                "总数"};
        titles.put(sheet1,title);
        Map<String,List<String[]>> values=new HashMap<>();
        List<String[]> value=new ArrayList<>();
        for(int i=0;i<list.size();i++){
            TypeTj typeTj = list.get(i);
            String[] str = new String[]{i+1+"",typeTj.getName(),
                    typeTj.getCount1()+"",typeTj.getCount2()+"",typeTj.getCount3()+"",typeTj.getCount4()+"",
                    typeTj.getCount5()+"",typeTj.getCount6()+"",typeTj.getCount7()+"",typeTj.getCount8()+"",
                    typeTj.getShycount()+"",typeTj.getCount9()+"",typeTj.getCount10()+"",typeTj.getCount11()+"",
                    typeTj.getCount12()+"",typeTj.getCount13()+"",typeTj.getCount14()+"",typeTj.getCount15()+"",
                    typeTj.getCzfcount()+"", typeTj.getCount16()+"",typeTj.getCount17()+"",typeTj.getCount18()+"",
                    typeTj.getCount19()+"", typeTj.getCount20()+"",typeTj.getCount21()+"",typeTj.getDdccount()+"",
                    typeTj.getTotalcount()+""};
            value.add(str);
        }
        values.put(sheet1,value);
        ExeclUtil.createExecl("类型统计表",sheets,titles,values,response);
    }
}
